//author by welong
//date 2015/11/04

//if MAXISZE==1000 cause test site 1&2 segmentation fault

#include <stdio.h>
#include <stdlib.h>

//#define DEBUG

#define MAXSIZE 1001

typedef struct
{
	long long llId;
	unsigned int uExamSeatNum;
}Stuinfo;

int main(int argc, char **argv)
{
	int N = 0;
	int M = 0;
	int i = 0;
	Stuinfo szRes[MAXSIZE] = {{0,0}};
	
	long long llId;
	unsigned int uTestSeatNum;
	unsigned int uExamSeatNum;

#ifdef DEBUG
	FILE *fp = fopen("data.txt", "r");
	if(NULL == fp)
	{
		printf("file open error\n");
		return 0;
	}
	fscanf(fp,"%u", &N);
#else
	scanf("%u", &N);
#endif

	for(i = 0; i < N; i++)
	{
#ifdef DEBUG
		fscanf(fp, "%llu%u%u", &llId, &uTestSeatNum, &uExamSeatNum);
#else
		scanf("%llu%u%u", &llId, &uTestSeatNum, &uExamSeatNum);
#endif

		szRes[uTestSeatNum].llId = llId;
		szRes[uTestSeatNum].uExamSeatNum = uExamSeatNum;
	}

#ifdef DEBUG
	fscanf(fp, "%u", &M);
#else
	scanf("%u", &M);
#endif

	for(i = 0; i < M; i++)
	{
#ifdef DEBUG
		fscanf(fp, "%u", &uTestSeatNum);
#else
		scanf("%u", &uTestSeatNum);
#endif

		printf("%llu %u\n", szRes[uTestSeatNum].llId, szRes[uTestSeatNum].uExamSeatNum);
	}
	return 0;
}